import router from '@ohos.router';
import featureAbility from '@ohos.ability.featureAbility';
import data_storage from '@ohos.data.storage';
import GameStorageConstant from '../../../model/GameStorageConstant.js'
const DELAY_TIME = 1500;

export default {
    data: {
        handlerId:0,
        GameStorageConstant : new GameStorageConstant(),
        storage:null,
        isCentralDevice:true // 是否为中心设备
    },
    onInit() {
        let self = this;
        // 获取页面传递的参数
        featureAbility.getWant().then((data) => {
            console.info(`[game] getWant data: ${JSON.stringify(data)}`);
            if (typeof(data.deviceId) != 'undefined' && data.deviceId.length > 0) {
                self.isCentralDevice = false;
            }
        })
    },

    grantPermission() {
        console.info(`[game] grantPermission`);
        let self = this;
        let context = featureAbility.getContext()
        context.requestPermissionsFromUser(['ohos.permission.DISTRIBUTED_DATASYNC'], 666, function (result) {
            console.info(`[game] grantPermission,requestPermissionsFromUser`);
            self.loadPage();
        })
    },

    onShow(){
        this.grantPermission();
    },

    /**
     * 根据用户是否设置用户名进行页面加载
     * 已设置-游戏设置页面  未设置-用户名输入页面
     * @param path
     */
    loadPage() {
        var context = featureAbility.getContext();
        let self = this;
        context.getFilesDir().then((path) => {
            console.info("get the storage path successful. path:" + JSON.stringify(path));
            const myStoragePath = self.GameStorageConstant.LOCAL_STORAGE_PATH;
            let promiseSt = data_storage.getStorage(path + myStoragePath);
            promiseSt.then((storage) => {
                self.storage = storage;
                let isHas = storage.hasSync(self.GameStorageConstant.KEY_NICK_NAME);
                console.info("get the storage. isHas:" + isHas);
                if (isHas) {
                    let nickNameVal = storage.getSync(self.GameStorageConstant.KEY_NICK_NAME, '');
                    if (nickNameVal != undefined && nickNameVal !== '' ) {
                        console.info('get the storage nick name:' + nickNameVal);
                        self.gotoSetting(nickNameVal);
                    } else {
                        console.error("get the storage, get nick name fail.");
                        self.gotoLogin();
                    }
                } else {
                    console.info('get the storage not exist');
                    this.gotoLogin();
                }
            }).catch((err) => {
                console.error('get the storage failed. err: ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.error('getFilesDir get the storage failed. err: ' + JSON.stringify(err));
            self.gotoLogin();
        });
    },

    gotoSetting(nickName) {
        let self = this;
        this.handlerId = setTimeout(function() {
            router.replace({
                url : "pages/setting/setting",
                params: {
                    nick_name : nickName,
                    is_central_device: self.isCentralDevice
                }
            })
        }, DELAY_TIME);
    },

    gotoLogin() {
        let self = this;
        this.handlerId = setTimeout(function() {
            router.replace({
                url : "pages/login/login",
                params: {
                    is_central_device: self.isCentralDevice
                }
            })
        }, DELAY_TIME);
    },

    onDestroy(){
        if (this.handlerId > 0) {
            clearTimeout(this.handlerId);
        }
    }
}
